package com.group6.blogplatform.web;

import com.group6.blogplatform.Bean.Comment;
import com.group6.blogplatform.Bean.User;
import com.group6.blogplatform.service.inter.BlogService;
import com.group6.blogplatform.service.inter.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        model.addAttribute("comments", commentService.listcommentByBlogId(blogId));
        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment) {
        System.out.println("+================");
        Long blogid = comment.getBlog().getId();
        comment.setBlog(blogService.getBlog(blogid));
        commentService.saveComment(comment);
        return "redirect:/comments/" + blogid;
    }
}
